﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jacaranda.Model;

namespace Jacaranda.Rules
{
    public class Unique : ValidationRule
    {
        public Unique()
        {
            Sequence = 1;
        }

        public override bool Validate(Entity entity, string propertyName, object propertyValue)
        {
            return Database.Count(entity.GetType(), 
                new AbstractProperty("Key") != Metadata.Type(entity.GetType()).PrimaryKey.GetValue(entity) &
                new AbstractProperty(propertyName) == propertyValue) == 0;
        }
    }
}
